package com.wollo.student.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wollo.domain.Check;
import com.wollo.domain.PageResult;

import com.wollo.student.mapper.CheckMapper;
import com.wollo.student.service.CheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author OuYang
 * @date 2024/9/1720:40
 * @description
 */
@Service
public class CheckServiceImpl implements CheckService {
    //依赖注入mapper层
    @Autowired
    private CheckMapper checkMapper;
    //依赖注入配置文件中的签到时间
    @Value("${sign.morning}")
    private String morningTime;
    @Value("${sign.moon}")
    private String moonTime;
    @Value("${sign.night}")
    private String nightTime;
    //用户签到

    @Override
    public int insCheckInfoService(Integer uid, String checkStatus) throws ParseException {
        //获取当前日期
        LocalDate checkDate=LocalDate.now();
        //获取当前系统的时间
        LocalTime now=LocalTime.now();
        DateTimeFormatter df=DateTimeFormatter.ofPattern("HH:mm:ss");
        String format= df.format(now);
        LocalTime parse=LocalTime.parse(format,df);
        //1.创建Check对象存储签到信息
        Check check = new Check();
        check.setUid(uid);
        check.setCheckStatus(checkStatus);
        check.setCheckDate(checkDate);
        //2.查询签到信息
        Check c = checkMapper.selectUserCheckInfoMapper(check);
        if (c!=null){
            return CheckService.CHECK_CODE_SUCCESS; //0 表示已经签到
        }
        //3.新增签到信息
        check.setCheckTime(parse);
        //判断签到结果
        int i = Integer.parseInt(checkStatus);
        int checkResult=0;
        //因为配置了过滤器，所以到达这里的时候，一定是正确的签到时间
        switch (i){
            case CheckService.CHECK_STATUS_MORNING: //早上签到
                checkResult=parse.isAfter(LocalTime.parse(morningTime, df))?CheckService.CHECK_RESULT_LATE:CheckService.CHECK_RESULT_NORMAL;
                break;
            case CheckService.CHECK_STATUS_MOON: //中午签到
                checkResult=parse.isAfter(LocalTime.parse(moonTime, df))?CheckService.CHECK_RESULT_LATE:CheckService.CHECK_RESULT_NORMAL;
                break;
            case CheckService.CHECK_STATUS_NIGHT: //晚上签到
                checkResult=parse.isAfter(LocalTime.parse(nightTime, df))?CheckService.CHECK_RESULT_LATE:CheckService.CHECK_RESULT_NORMAL;
                break;
        }
        check.setCheckResult(checkResult+"");
        System.out.println(check.toString());
        checkMapper.insertUserCheckInfoMapper(check);
        return CheckService.ADD_CHECK_CODE_SUCCESS;

    }
    //分摊也查询当前用户的签到信息
    @Override
    public PageResult<Check> selectUserCheckInfoService(Integer pageNum, Integer pageSize, Integer uid, String checkDate) {
        //开启分页信息
        Page<Check> page = PageHelper.startPage(pageNum, pageSize);
        //调用mapper完成查询
        List<Check> checks = checkMapper.selectUserCheckInfoMapperList(uid, checkDate);
        //封装返回
        PageResult<Check> pageResult=new PageResult<>();
        //这里设置code=0的原因是layui的分页插件要求返回的code必须为0才表示成功
        pageResult.setCode(0);
        pageResult.setData(checks);//设置数据
        pageResult.setCount(page.getTotal());//设置总数
        return pageResult;
    }
}
